package com.sxkf.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxkf.template.entity.AmountRecord;
import com.sxkf.template.entity.PayoutRecord;
import com.sxkf.template.entity.User;
import com.sxkf.template.mapper.AmountRecordMapper;
import com.sxkf.template.mapper.PayoutRecordMapper;
import com.sxkf.template.mapper.UserMapper;
import com.sxkf.template.service.PayoutRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunzhanghu.sdk.payment.domain.CreateAlipayOrderRequest;
import org.jeecg.common.exception.SxkfException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-12-03 20:05:24
 */
@Service
public class PayoutRecordServiceImpl extends ServiceImpl<PayoutRecordMapper, PayoutRecord> implements PayoutRecordService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private AmountRecordMapper amountRecordMapper;

    @Override
    public synchronized void apply(PayoutRecord payoutRecord) {
        User user = userMapper.selectById(payoutRecord.getUserId());

        if (user.getAmount().compareTo(payoutRecord.getPrice())<0){
            throw new SxkfException("剩余现金不足");
        }

        if (!Objects.equals("1",user.getIsAuth())){
            throw new SxkfException("未完成实名认证");
        }

        //扣减用户积分
        user.setAmount(user.getAmount().subtract(payoutRecord.getPrice()));
        userMapper.updateById(user);

        //添加提现申请记录
        payoutRecord.setState("0");
        save(payoutRecord);

        //添加余额变动记录
        AmountRecord amountRecord = new AmountRecord();
        amountRecord.setUserId(payoutRecord.getUserId());
        amountRecord.setPrice(payoutRecord.getPrice());
        amountRecord.setIsAdd("0");
        amountRecord.setRemark("审核提现");
        amountRecordMapper.insert(amountRecord);
    }

    @Override
    public void rejected(String id) {
        PayoutRecord payoutRecord = getById(id);

        handleFail(payoutRecord,null);
    }

    @Override
    public void pass(String id) {
        PayoutRecord payoutRecord = getById(id);
        User user = userMapper.selectById(payoutRecord.getUserId());

        //todo 给用户转账（需确认用哪家分账系统）

        //修改申请状态
        payoutRecord.setState("1");
        updateById(payoutRecord);
    }

    private void handleFail(PayoutRecord payoutRecord,String reason){
        //返还用户积分
        User user = userMapper.selectById(payoutRecord.getUserId());
        user.setAmount(user.getAmount().add(payoutRecord.getPrice()));
        userMapper.updateById(user);

        //添加余额变动记录
        AmountRecord amountRecord = new AmountRecord();
        amountRecord.setUserId(payoutRecord.getUserId());
        amountRecord.setPrice(payoutRecord.getPrice());
        amountRecord.setIsAdd("1");
        amountRecord.setRemark("提现退回");
        amountRecordMapper.insert(amountRecord);

        //修改申请状态
        payoutRecord.setState(reason == null?"2":"4");
        updateById(payoutRecord);
    }
}
